Ext.define("HRMobile.controller.EmployeeController", {
    extend: "Ext.app.Controller",
	
    config:  {
    	stores : ['EmployeeStore',
    	          'EmergencyContactsStore',
    	          'DependentStore',
    	          'JobsStore',
    	          'SalariesStore',
    	          'SupervisorStore',
    	          'SubordinateStore',
    	          'ExperienceStore',
    	          'EducationStore',
    	          'SkillStore',
    	          'LanguageStore',
    	          'LicenseStore'],
        models : ['Employee',
                  'EmergencyContact',
                  'Dependent',
                  'Job',
                  'Salary',
                  'Experience',
                  'Education',
                  'Skill',
                  'Language',
                  'License'],

        refs: {
        	listEmployee: '#listEmployee',
        	leftNavigator: 'list#leftNavigator',
        	jobsList : 'list#jobsList',
        	experienceList: 'list#experienceList',
        	educationList: 'list#educationList',
        	languageList : 'list#languageList',
        	emergencyContactList: 'list#emergencycontactlist',
        	licenseList : 'list#licenseList'
        },
        control: {
        	listEmployee: {
        		itemtap : function(list, index, target, record){
        			
        			var nvEmployee = Ext.getCmp('nvEmployee');
        			var empNumber = record.data.empNumber;
        			var empFullName = record.data.empLastname +' '+ record.data.empMiddleName +' '+ record.data.empFirstname;
        			bindSourceDetailsStore(empNumber,'EmergencyContactsStore');
        			bindSourceDetailsStore(empNumber,'DependentStore');
        			bindSourceDetailsStore(empNumber,'JobsStore');
        			bindSourceDetailsStore(empNumber,'SalariesStore');
        			bindSourceDetailsStore(empNumber,'SupervisorStore');
        			bindSourceDetailsStore(empNumber,'SubordinateStore');
        			bindSourceDetailsStore(empNumber,'ExperienceStore');
        			bindSourceDetailsStore(empNumber,'EducationStore');
        			bindSourceDetailsStore(empNumber,'SkillStore');
        			bindSourceDetailsStore(empNumber,'LanguageStore');
        			bindSourceDetailsStore(empNumber,'LicenseStore');
        			var detail = Ext.create(HRMobile.view.EmployeeDetailForm,{
        				title: empFullName +'\'s Employee Details',
        				id: 'pnlEmployeeDetails',
        			});
        			detail.element.on (
        				'swipe',function(e){
        					var startLimit = 250;
        					if (e.direction == 'left'){
        						if (e.startX < startLimit + 15)
        							Ext.getCmp('leftNavigator').hide();
        					}
        					else if (e.startX < 15)
        							Ext.getCmp('leftNavigator').show();

        				}
        			);
        			Ext.getCmp('btnDetailsCallHome').setHtml('<a href="tel:'+record.data.empMobile+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
        			Ext.getCmp('btnDetailsCallMobile').setHtml('<a href="tel:'+record.data.empMobile+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
        			Ext.getCmp('btnDetailsCallWork').setHtml('<a href="tel:'+record.data.empMobile+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
        			nvEmployee.push(detail);
        			detail.reset();
        			detail.setRecord(record);
        			
        			
                },
            },
            leftNavigator: {
            	itemtap: function(view, index, item, e) {
            	  Ext.getCmp('pnlEmployeeDetails').setActiveItem(index);
            	  if (Ext.os.is.Phone)
						Ext.getCmp('leftNavigator').hide();

            	}
            },
            jobsList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('jobDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            experienceList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('experienceDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            educationList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('educationDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            languageList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('languageDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            emergencyContactList: {
            	itemtap: function(list, index, target, record) {
            		Ext.getCmp('btnCallMobile').setHtml('<a href="tel:'+record.data.ecMobileNo+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
            		Ext.getCmp('btnCallHome').setHtml('<a href="tel:'+record.data.ecHomeNo+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
            		Ext.getCmp('btnCallOffice').setHtml('<a href="tel:'+record.data.ecOfficeNo+'" class="x-button x-iconalign-center x-button-action x-layout-box-item x-flexed x-stretched" target="_blank"><span class="x-button-icon phone1 x-icon-mask" style="" id="ext-element-606">&nbsp;</span></a>');
            		var detail = Ext.getCmp('emergencyContactsDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            licenseList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('licenseDetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            }
        }
    },
});
var bindSourceDetailsStore = function(empNumber,storeName){
 	var store = Ext.getStore(storeName);
 	store.getProxy().setExtraParam('emp_number',empNumber);
 	store.load();
};
